package com.maiyuren.fishkit.history

import com.maiyuren.fishkit.utils.FishStorage
import org.json.JSONArray
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Vector


class FavouriteManager private constructor() {
    inner class GroupItem : Vector<String?>() {
        private var file_time: Long = 0
        var group: String? = null
        var fileTime: Long
            get() = file_time
            set(fileTime) {
                val d = Date(fileTime)
                val dateFormat = SimpleDateFormat("yyyy-MM-dd")
                group = dateFormat.format(d)
                file_time = fileTime
            }
    }

    var words_ = Vector<GroupItem>()
    init {
        read()
    }

    fun add(word: String): Boolean {
        if (isWordInFavorite(word)) return false
        if (!hasTodayItem()) {
            addToDayDefault()
        }
        words_.firstElement().add(0, word)
        save()
        return true
    }

    fun isWordInFavorite(word: String): Boolean {
        for (group in words_) {
            for (item in group) {
                if (item == word) {
                    return true
                }
            }
        }
        return false
    }

    fun delete(word: String): Boolean {
        for (group in words_) {
            for (i in group.indices) {
                val item = group.elementAt(i)
                if (item == word) {
                    group.removeAt(i)
                    save()
                    return true
                }
            }
        }
        return false
    }

    fun clear() {
        words_.clear()
        save()
    }

    private fun save() {
        try {
            val js_root = JSONObject()
            if (words_.size > 0) {
                val groups_js = JSONArray()
                for (group in words_) {
                    val group_js = JSONObject()
                    if (group.size > 0) {
                        group_js.put("date", group.fileTime)
                        val words_js = JSONArray()
                        for (i in group.indices) {
                            val item = group.elementAt(i)
                            words_js.put(item)
                        }
                        group_js.put("words", words_js)
                        groups_js.put(group_js)
                    }
                }
                js_root.put("root", groups_js)
                FishStorage.setJson(FishStorage.keys.favorite_words, js_root)
            } else {
                FishStorage.setJson(FishStorage.keys.favorite_words, null)
            }
        } catch (ex: Exception) {
        }
    }

    private fun read() {
        val jsonText: String? = FishStorage.getString(FishStorage.keys.favorite_words)
        if (jsonText == null) {
            addToDayDefault()
            return
        }
        try {
            val js = JSONObject(jsonText)
            if (js != null) {
                try {
                    val groups_js = js.getJSONArray("root")
                    if (groups_js != null) {
                        for (i in 0 until groups_js.length()) {
                            val group_js = groups_js.getJSONObject(i)
                            val words_js = group_js.getJSONArray("words")
                            if (words_js != null && words_js.length() > 0) {
                                val favorite_group = GroupItem()
                                favorite_group.fileTime = group_js.getLong("date")
                                for (j in 0 until words_js.length()) {
                                    val word_itemStr = words_js.getString(j)
                                    favorite_group.add(word_itemStr)
                                }
                                words_.add(favorite_group)
                            }
                        }
                    }
                } catch (ex: Exception) {
                }
                if (!hasTodayItem()) {
                    addToDayDefault()
                }
            } else {
                addToDayDefault()
            }
        } catch (ex: Exception) {
        }
    }

    private fun hasTodayItem(): Boolean {
        return if (words_.size > 0) {
            try {
                val tempItem = GroupItem()
                tempItem.fileTime = System.currentTimeMillis()
                if (words_.firstElement().group == tempItem.group) {
                    true
                } else false
            } catch (ex: Exception) {
                false
            }
        } else false
    }

    private fun addToDayDefault() {
        val item = GroupItem()
        item.fileTime = System.currentTimeMillis()
        words_.add(0, item)
    }

    companion object {
        private var self_: FavouriteManager? = null
        fun instance(): FavouriteManager {
            if (self_ == null) {
                self_ = FavouriteManager()
            }
            return self_!!
        }
    }
}
